Public Class CreateNewFRM
#Region "Basic UI"
    Private Sub chkF_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkF.CheckedChanged
        txtF.Enabled = chkF.Checked
        optF.Enabled = chkF.Checked

    End Sub

    Private Sub chkE_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkE.CheckedChanged
        txtE.Enabled = chkE.Checked
        optE.Enabled = chkE.Checked

        a = chkE.Checked
        'chkD.Checked = a
        'chkE.Checked = a
        chkF.Checked = a
        'txtD.Enabled = chkD.Checked
        'optD.Enabled = chkD.Checked

        'txtE.Enabled = chkE.Checked
        'optE.Enabled = chkE.Checked

        txtF.Enabled = chkF.Checked
        optF.Enabled = chkF.Checked
    End Sub

    Private Sub chkD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkD.CheckedChanged
        txtD.Enabled = chkD.Checked
        optD.Enabled = chkD.Checked
        a = chkD.Checked

        chkE.Checked = a
        chkF.Checked = a
        'txtD.Enabled = chkD.Checked
        'optD.Enabled = chkD.Checked

        txtE.Enabled = chkE.Checked
        optE.Enabled = chkE.Checked

        txtF.Enabled = chkF.Checked
        optF.Enabled = chkF.Checked
    End Sub

    Private Sub chkC_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkC.CheckedChanged
        txtC.Enabled = chkC.Checked
        optC.Enabled = chkC.Checked

        a = chkC.Checked
        chkD.Checked = a
        chkE.Checked = a
        chkF.Checked = a
        txtD.Enabled = chkD.Checked
        optD.Enabled = chkD.Checked

        txtE.Enabled = chkE.Checked
        optE.Enabled = chkE.Checked

        txtF.Enabled = chkF.Checked
        optF.Enabled = chkF.Checked
    End Sub

    Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optTF.CheckedChanged
        GroupBox3.Controls.Add(PnTF)
        PnTF.Location = New Point(5, 15)
        PnTF.Visible = True
        PnMulti.Visible = False
        txtWriteIn.Visible = False
    End Sub

    Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optMulti.CheckedChanged
        GroupBox3.Controls.Add(PnMulti)
        PnMulti.Location = New Point(5, 15)
        PnTF.Visible = False
        PnMulti.Visible = True
        txtWriteIn.Visible = False
    End Sub

    Private Sub PnMulti_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles PnMulti.Scroll
        GroupBox3.Refresh()
    End Sub

    Private Sub optWrite_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optWrite.CheckedChanged
        txtWriteIn.Visible = True
        PnMulti.Visible = False
        PnTF.Visible = False
    End Sub
#End Region

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim q As New Question
        Dim A As New Answer
        Dim lst As New List(Of String)
        lst.Add("")
        A.Answers = lst
        q.Question = txtquestion.Text
        If optMulti.Checked = True Then
            A.type = QuestionType.MultipleChoice
            q.Type = QuestionType.MultipleChoice
            If txtF.Enabled = True Then
                A.Answers.Add(txtA.Text)
                A.Answers.Add(txtB.Text)
                A.Answers.Add(txtC.Text)
                A.Answers.Add(txtD.Text)
                A.Answers.Add(txtE.Text)
                A.Answers.Add(txtF.Text)
            ElseIf txtE.Enabled = True Then
                A.Answers.Add(txtA.Text)
                A.Answers.Add(txtB.Text)
                A.Answers.Add(txtC.Text)
                A.Answers.Add(txtD.Text)
                A.Answers.Add(txtE.Text)
            ElseIf txtD.Enabled = True Then
                A.Answers.Add(txtA.Text)
                A.Answers.Add(txtB.Text)
                A.Answers.Add(txtC.Text)
                A.Answers.Add(txtD.Text)
            ElseIf txtC.Enabled = True Then
                A.Answers.Add(txtA.Text)
                A.Answers.Add(txtB.Text)
                A.Answers.Add(txtC.Text)

            End If
            If optA.Checked = True Then
                A.CorrectIndex = 0
            ElseIf optB.Checked = True Then
                A.CorrectIndex = 1
            ElseIf optC.Checked = True Then
                A.CorrectIndex = 2
            ElseIf optD.Checked = True Then
                A.CorrectIndex = 3
            ElseIf optE.Checked = True Then
                A.CorrectIndex = 4
            ElseIf optF.Checked = True Then
                A.CorrectIndex = 5
            End If
        ElseIf optTF.Checked = True Then
            A.type = QuestionType.TrueFalse
            q.Type = QuestionType.TrueFalse
            A.Answers.Add("True")
            A.Answers.Add("False")
            If optTrue.Checked = True Then
                A.CorrectIndex = 0
            Else : A.CorrectIndex = 1
            End If
        ElseIf optWrite.Checked = True Then
            A.type = QuestionType.WriteIn
            q.Type = QuestionType.WriteIn
            A.Answers.Add(txtWriteIn.Text)
            A.CorrectIndex = 0

        End If
        A.Answers.RemoveAt(0)
        'A.Answers.RemoveAt(A.Answers.Count - 1)

        q.Answer = A
        q.ID = GetID()
        If lblIndex.Text = "" Then
            Questions.Add(q)
        Else : ind = Val(lblIndex.Text)
            Questions.RemoveAt(ind)
            Questions.Insert(ind, q)
            lblIndex.Text = ""
        End If

        UpdateQuestions()
        ClearAll()
    End Sub
    Public Sub UpdateQuestions()
        lstQuestions.Items.Clear()
        If Questions.Count = 0 Then
            Exit Sub
        End If

        For Each q As Question In Questions
            lstQuestions.Items.Add(q.Question)
        Next
    End Sub

    Private Sub CreateNewFRM_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lstQuestions.Items.Clear()
        txtquestion.Text = ""
        txtWriteIn.Text = ""

        lblIndex.Text = ""
        UpdateQuestions()
        UpdateColorGradient()
        CreateBackGradient()

        '*****************update answer type*********************
        GroupBox3.Controls.Add(PnTF)
        PnTF.Location = New Point(5, 15)
        PnTF.Visible = True
        PnMulti.Visible = False
        txtWriteIn.Visible = False
        '********************************************************

    End Sub

    Private Sub lstQuestions_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstQuestions.SelectedIndexChanged
        If lstQuestions.SelectedItems.Count = 0 Then
            cmdEdit.Enabled = False
            cmdDelete.Enabled = False
            Exit Sub
        End If
        cmdEdit.Enabled = True
        cmdDelete.Enabled = True
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        If Val(lblIndex.Text) = lstQuestions.SelectedIndex Then
            lblIndex.Text = ""
        End If
        Questions.RemoveAt(lstQuestions.SelectedIndex)
        lstQuestions.Items.RemoveAt(lstQuestions.SelectedIndex)

        UpdateQuestions()
    End Sub
    Public Sub ClearAll()
        txtquestion.Text = ""
        txtWriteIn.Text = ""
        txtA.Text = ""
        txtB.Text = ""
        txtC.Text = ""
        txtD.Text = ""
        txtE.Text = ""
        txtF.Text = ""

    End Sub
    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        ClearAll()
        index = lstQuestions.SelectedIndex
        Dim q As New Question
        Dim A As New Answer
        q = Questions.Item(index)
        A = q.Answer
        correct = q.Answer.CorrectIndex
        If q.Type = QuestionType.MultipleChoice Then
            optMulti.Checked = True
            GroupBox3.Controls.Add(PnMulti)
            PnMulti.Location = New Point(5, 15)
            PnTF.Visible = False
            PnMulti.Visible = True
            txtWriteIn.Visible = False

            Select Case correct
                Case 0
                    optA.Checked = True
                Case 1
                    optB.Checked = True
                Case 2
                    optC.Checked = True
                Case 3
                    optD.Checked = True
                Case 4
                    optE.Checked = True
                Case 5
                    optF.Checked = True
            End Select
            Try
                txtA.Text = A.Answers.Item(0)
                txtB.Text = A.Answers.Item(1)
                txtC.Text = A.Answers.Item(2)
                txtD.Text = A.Answers.Item(3)
                txtE.Text = A.Answers.Item(4)
                txtF.Text = A.Answers.Item(5)
            Catch ex As Exception
            End Try

        ElseIf q.Type = QuestionType.TrueFalse Then
            optTF.Checked = True
            GroupBox3.Controls.Add(PnTF)
            PnTF.Location = New Point(5, 15)
            PnTF.Visible = True
            PnMulti.Visible = False
            txtWriteIn.Visible = False
            Select Case correct
                Case 0
                    optTrue.Checked = True
                Case 1
                    optFalse.Checked = True
            End Select
        Else
            optWrite.Checked = True
            txtWriteIn.Visible = True
            PnMulti.Visible = False
            PnTF.Visible = False
            txtWriteIn.Text = A.Answers.Item(0)
        End If
        lblIndex.Text = lstQuestions.SelectedIndex
        txtquestion.Text = q.Question


    End Sub

    Private Sub PnMulti_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PnMulti.VisibleChanged
        PnMulti.Size = New Point(GroupBox3.Size.Width - 15, GroupBox3.Size.Height - 16)
    End Sub

    Private Sub PnTF_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PnTF.VisibleChanged
        PnTF.Size = New Point(GroupBox3.Size.Width - 15, GroupBox3.Size.Height - 16)

    End Sub

    Private Sub GroupBox3_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GroupBox3.SizeChanged
        PnMulti.Size = New Point(GroupBox3.Size.Width - 15, GroupBox3.Size.Height - 16)
        PnTF.Size = New Point(GroupBox3.Size.Width - 15, GroupBox3.Size.Height - 16)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '<{question;ID;type[answer1|answer2|answer3|]}>
        Dim a As String = "<"
        For Each q As Question In Questions
            a = a & "{" & q.Question & ";" & q.ID & ";" & q.Type & "["
            Dim ans As Answer = q.Answer
            For Each s As String In ans.Answers
                a = a & s & "|"
            Next
            a = a & "]" & ans.CorrectIndex & "}"
        Next
        a = a & ">"
        Dim lst As New List(Of String)
        lst.Add(a)
        b = "<"
        If BackImage.Checked = True Then
            b = "i:" & ImageLocation.Text
        ElseIf BackColor.Checked = True Then
            b = "c:" & SolidColor.BackColor.R & "," & SolidColor.BackColor.G & "," & SolidColor.BackColor.B
        ElseIf BackGradient.Checked = True Then
            Dim c1 As Color = pbBackColor1.BackColor
            Dim c2 As Color = PbBackColor2.BackColor
            b = "g:" & c1.R & "," & c1.G & "," & c1.B & "," & c2.R & "," & c2.G & "," & c2.B & ":" & GradientAngle.Value
        End If
        b = b & ">"
        lst.Add(b)
        c = "<"
        Dim c3 As Color = pbColor1.BackColor
        Dim c4 As Color = pbColor2.BackColor
        c = c & c3.R & "," & c3.G & "," & c3.B & "," & c4.R & "," & c4.G & "," & c4.B
        c = c & ">"
        lst.Add(c)
        lst.Add(RandomizeOrder.Checked)

        Dim FileContents As String = GetSave(lst, "~")
        Dim retu As DialogResult
        retu = Save1.ShowDialog

        If retu = Windows.Forms.DialogResult.Cancel Then
            Exit Sub
        Else : My.Computer.FileSystem.WriteAllText(Save1.FileName, FileContents, False)
        End If

        'a now contains all the questions and answers
        Dim dir As String = My.Application.Info.DirectoryPath & "/rec.txt"
        My.Computer.FileSystem.WriteAllText(dir, Save1.FileName & ",", True)
        Form1.LoadRecent()

        Dim result As MsgBoxResult = MsgBox("Would you like to play this game now?", MsgBoxStyle.YesNoCancel, "Play Game?")
        If result = MsgBoxResult.Cancel Then
            Exit Sub
        ElseIf result = MsgBoxResult.No Then
            Form1.Show()
            Me.Close()
        Else
            Form1.Show()

            FullScreen.Filename = Save1.FileName
            FullScreen.Show()
            Me.Close()
        End If
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Color1.ShowDialog()
        pbColor1.BackColor = Color1.Color
        UpdateColorGradient()
    End Sub
    Public Sub UpdateColorGradient()
        Dim bmp As New Bitmap(pbSample.Width, pbSample.Height)
        Dim b1 As New Drawing2D.LinearGradientBrush(New Rectangle(0, 0, bmp.Width, bmp.Height), pbColor1.BackColor, pbColor2.BackColor, Drawing2D.LinearGradientMode.Vertical)
        Dim tb As New Drawing2D.HatchBrush(Drawing2D.HatchStyle.Percent75, Color.Gray, Color.Transparent)
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.FillRectangle(b1, New Rectangle(0, 0, bmp.Width, bmp.Height))
        pbSample.Image = bmp
        Dim bmp1 As New Bitmap(bmp.Width, bmp.Height)
        Dim g1 As Graphics = Graphics.FromImage(bmp1)
        g1.FillRectangle(b1, New Rectangle(0, 0, bmp.Width, bmp.Height))
        g1.FillRectangle(tb, New Rectangle(0, 0, bmp.Width, bmp.Height))
        pbSample1.Image = bmp1
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Color1.ShowDialog()
        pbColor2.BackColor = Color1.Color
        UpdateColorGradient()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Color1.ShowDialog()
        pbBackColor1.BackColor = Color1.Color
        CreateBackGradient()
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Color1.ShowDialog()
        PbBackColor2.BackColor = Color1.Color
        CreateBackGradient()
    End Sub
    Public Sub CreateBackGradient()
        Dim bmp As New Bitmap(pbGradient.Width, pbGradient.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim b1 As New Drawing2D.LinearGradientBrush(New Rectangle(0, 0, bmp.Width, bmp.Height), pbBackColor1.BackColor, PbBackColor2.BackColor, GradientAngle.Value)
        g.FillRectangle(b1, New Rectangle(0, 0, bmp.Width, bmp.Height))
        pbGradient.Image = bmp

    End Sub

    Private Sub GradientAngle_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GradientAngle.ValueChanged
        CreateBackGradient()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Color1.ShowDialog()
        SolidColor.BackColor = Color1.Color
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim a As DialogResult
        a = Open1.ShowDialog
        If a = Windows.Forms.DialogResult.Cancel Then
            Exit Sub
        End If
        ImageLocation.Text = Open1.FileName

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Form1.Show()
        Me.Close()
    End Sub
End Class